package chabernac.android.wordbattle;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import chabernac.android.wordbattle.paint.DrawableComposite;
import chabernac.android.wordbattle.paint.gameselection.SelectionDrawable;

public class WordBattleSelectionView extends View {
  private static final int BORDER = 60; 
  private DrawableComposite myDrawableComposite = null;

  public WordBattleSelectionView(Context aContext, AttributeSet aAttrs, int aDefStyle) {
    super(aContext, aAttrs, aDefStyle);
    init();
  }

  public WordBattleSelectionView(Context aContext, AttributeSet aAttrs) {
    super(aContext, aAttrs);
    init();
  }

  public WordBattleSelectionView(Context aContext) {
    super(aContext);
    init();
  }
  
  private void init(){
    setMinimumHeight( 2000 );
    setOnTouchListener(new TouchListener());
    
  }
  
  private void createDrawableComposite(){
    myDrawableComposite = new DrawableComposite();
    int theWidth = getWidth() - 2 * BORDER;
    int theHeight = (int)(theWidth * 0.7);
    myDrawableComposite.addDrawable(new SelectionDrawable(BORDER, BORDER, theWidth, theHeight, 150));
    myDrawableComposite.addDrawable(new SelectionDrawable(BORDER, BORDER * 2 + theHeight, theWidth, theHeight, 150));
  }
 
  @Override
  protected void onDraw(Canvas aCanvas) {
     super.onDraw(aCanvas);
     
     Paint thePaint = new Paint();
     thePaint.setColor(Color.WHITE);
     thePaint.setAntiAlias(true);
     aCanvas.drawRect(0,0,getWidth(),getHeight(), thePaint);
     
//     thePaint.setColor(Color.BLUE);
//     for(int y=0;y<2000;y += 197){
//       thePaint.setColor(Color.argb( 255, 100, 100, y % 255 ));
//       aCanvas.drawCircle( getWidth() / 2, y, 50, thePaint );
//     }
     
     if(myDrawableComposite == null){
       createDrawableComposite();
     }
     
     myDrawableComposite.paint(aCanvas, this);
  }
  
  private void startWordBattle(){
    Intent settingsActivity = new Intent(getContext(), WordBattleActivity.class);
    getContext().startActivity(settingsActivity);
  }

  public class TouchListener implements OnTouchListener {

    @Override
    public boolean onTouch(View aArg0, MotionEvent anEvent) {
      if(anEvent.getAction() == MotionEvent.ACTION_UP && anEvent.getDownTime() <  60287347){
          startWordBattle();
      }
      return true;
    }
  }  
}
